$(function () {
    // 头部分类导航显示与隐藏
    var isShow = false
    $('.header-menu').on('click', function () {
        if (isShow) {
            $('.category-header-wrapper').hide()
            isShow = false
        } else {
            isShow  = true
            $('.category-header-wrapper').show()
        }
    })

    // 点击阴影部分隐藏下拉分类导航
    $('body').on('click', function(e) {
        if (e.target.className === 'category-header-wrapper') {
            isShow = false
            $('.category-header-wrapper').hide()
        }
    });


    $('.arrow').on('click', function () {
        isShow = false
        $('.category-header-wrapper').hide()
    })

    // 回到顶部
    $('.to-top').on('click', function () {
        $(window).scrollTop(0)
    })

    // 轮播图
    var mySwiper = new Swiper ('.swiper-container', {
        autoplay: 4000,
        loop: true,
        effect : 'fade',
        pagination: '.swiper-pagination'
    })

    // 跳转搜索页
    $('.search-mask').on('click', function (e) {
        e.preventDefault()
        window.location.href = '/searchs'
    })


    var PAGENUM_KEY = '__pageNum__'
    var isFirstLoad = false
    var href = window.location.href
    var str = href.substr(href.lastIndexOf('/') + 1) ? href.substr(href.lastIndexOf('/') + 1) : '/'
    var top = Number(sessionStorage.getItem(str))

    if (top === 0) {
        sessionStorage.setItem(str + PAGENUM_KEY, 0)
    }
    var pageNum = Number(sessionStorage.getItem(str + PAGENUM_KEY))

    if (pageNum > 0) {
        $.get('/hotgoods?pageNum=' + pageNum + '&all=all', function (data) {
            var goods = data.content.splice(20)
            var htmls = []
            goods.forEach(function (good, index) {
                var fragment = Number(good.price) === Number(good.priceDiscount)
                    ? '<span>原价</span><span class="coupon-price">￥' + good.price + '</span>'
                    : '<span>券后</span><span class="coupon-price">￥' + good.priceDiscount + '</span>'

                var html =
                    '<div class="commodity-item">' +
                    '<a href="/good/' +  good.goodsId + '" > ' +
                    '<input type="hidden" id="goodsId" value="' + good.goodsId + '"/>' +
                    '<img class="lazy" src="http://owtl83r0c.bkt.clouddn.com/placeholder.gif" data="' + good.image + '"/>' +
                    '<span class="commodity-title">' + good.name + '</span> ' +
                    '<div class="commodity-coupon-info"> ' + fragment +
                    '<span class="alreadyBuy">' + good.alreadyBuy + '人已买</span> ' +
                    '</div> ' +
                    '</a> ' +
                    '<span class="hot">' +
                    '<img src="http://owtl83r0c.bkt.clouddn.com/hot.png" />' +
                    '</span>' +
                    '<span class="coupon-banner">' +
                    '<span class="coupon-banner-left">' + '￥' + good.discount + '.0' + '</span>' +
                    '<span class="coupon-banner-right">领券</span>' +
                    '</span>' +
                    '</div>'
                htmls.push(html)
            })
            $('.commodity-list').append(htmls.join(''))
            $('img.lazy').lazyLoad()
            setTimeout(function () {
                $(window).scrollTop(top)
                isFirstLoad = true
                $('.commodity-list').on('click', 'a', function (e) {
                    var id = $(this).find('#goodsId').val()
                    e.preventDefault()
                    window.location.href = '/good/' + id
                    setScrollTop()
                })
            }, 200)

        })
    } else {
        isFirstLoad = true
    }

    $('.commodity-list').on('click', 'a', function (e) {
        var id = $(this).find('#goodsId').val()
        e.preventDefault()
        window.location.href = '/good/' + id
        setScrollTop()
    })


    function setScrollTop () {
        var href = window.location.href
        var str = href.substr(href.lastIndexOf('/') + 1) ? href.substr(href.lastIndexOf('/') + 1) : '/'
        var scrollTop = $(window).scrollTop()
        sessionStorage.setItem(str, scrollTop)
    }

    // 下拉加载
    var windowHeight = $(window).height(),
        windowScrollTop = 0,
        documentHeight = 0,
        isLoad = false,
        isMore = true
    $(window).on('scroll', function () {
        windowScrollTop = $(window).scrollTop()
        documentHeight = $(document).height()
        if (windowScrollTop + windowHeight >= documentHeight && !isLoad && isMore && isFirstLoad) {
            loadMore()
        }
        // 控制小火箭显示影藏
        if (windowScrollTop > 0) {
            $('.to-top').css('display', 'flex')
        } else {
            $('.to-top').css('display', 'none')
        }
    })

    function loadMore () {
        pageNum = Number(sessionStorage.getItem(str + PAGENUM_KEY)) + 1
        sessionStorage.setItem(str + PAGENUM_KEY, pageNum)
        isLoad = true
        $('#loading').show()
        $.get('/hotgoods?pageNum=' + pageNum, function (data) {
            isLoad = false
            var goods = data.content
            if (goods.length === 0) {
                $('#loading').hide()
                $('#info').show()
                isMore = false
                return
            }
            var htmls = []
            goods.forEach(function (good, index) {
                var fragment =  Number(good.price) === Number(good.priceDiscount)
                    ? '<span>原价</span><span class="coupon-price">￥' + good.price + '</span>'
                    : '<span>券后</span><span class="coupon-price">￥' + good.priceDiscount + '</span>'

                var html =
                    '<div class="commodity-item">' +
                        '<a href="/good/' +  good.goodsId + '" > ' +
                            '<input type="hidden" id="goodsId" value="' + good.goodsId + '"/>' +
                            '<img class="lazy" src="http://owtl83r0c.bkt.clouddn.com/placeholder.gif" data="' + good.image + '"/>' +
                            '<span class="commodity-title">' + good.name + '</span> ' +
                            '<div class="commodity-coupon-info"> ' + fragment +
                                '<span class="alreadyBuy">' + good.alreadyBuy + '人已买</span> ' +
                            '</div> ' +
                        '</a> ' +
                        '<span class="hot">' +
                            '<img src="http://owtl83r0c.bkt.clouddn.com/hot.png" />' +
                        '</span>' +
                        '<span class="coupon-banner">' +
                            '<span class="coupon-banner-left">' + '￥' + good.discount + '.0' + '</span>' +
                            '<span class="coupon-banner-right">领券</span>' +
                        '</span>' +
                    '</div>'
                htmls.push(html)
            })
            $('#loading').hide()
            $('.commodity-list').append(htmls.join(''))
            $('img.lazy').lazyLoad()
            $('.commodity-list').on('click', 'a', function (e) {
                var id = $(this).find('#goodsId').val()
                e.preventDefault()
                window.location.href = '/good/' + id
                setScrollTop()
            })
        })
    }

    // 图片懒加载:youhui.lazyLoad.js
    $('img.lazy').lazyLoad()
})